define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jeek/category/index' + location.search,
                    add_url: 'jeek/category/add',
                    edit_url: 'jeek/category/edit',
                    del_url: 'jeek/category/del',
                    multi_url: 'jeek/category/multi',
                    import_url: 'jeek/category/import',
                    table: 'jeek_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', align: 'left', formatter: function (value, row, index) {
                            return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                        }},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'is_hidden',
                            title: __('状态'),
                            align: 'center',
                            formatter: function (value, row) {
                             // 手动生成带有data-url属性的按钮
                             var url = 'jeek/category/toggle_hide';
                             var id = row.id;
                             var title = value === 1 ? __('Hide') : __('Show');
                             var icon = value === 1 ? 'fa fa-eye-slash' : 'fa fa-eye';
                             var className = value === 1 ? 'btn btn-xs btn-danger' : 'btn btn-xs btn-success';
                             return '<a href="javascript:;" class="toggle ' + className + '" data-id="' + id + '" data-url="' + url + '" data-toggle="tooltip" data-original-title="' + title + '"><i class="' + icon + '"></i></a>';
                         },
                         events: {
                            'click .toggle': function (e, value, row) {
                                e.stopPropagation();
                                var url = $(this).data('url');
                                var id = $(this).data('id');
                                $.post(url, {id: id}, function (res) {
                                    if (res.code === 1) {
                                        // Layer.msg(res.msg, {icon: 1}); // 显示成功提示
                                        // Table.api.refresh(); // 刷新表格
                                        table.bootstrapTable('refresh');
                                    } else {
                                        Layer.msg(res.msg, {icon: 2}); // 显示错误提示
                                    }
                                });
                            }
                        }
                    },
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        // {field: 'status', title: __('Status'), searchList: {"30": __('Status 30')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 正确的批量操作绑定位置
            table.on('post-body.bs.table', function () {
                if ($(".btn-batch-hide").length === 0) {
                $(".btn-group .dropdown-menu").append([
                    '<li><button type="button" class="btn btn-warning btn-batch-hide" title="批量隐藏平台分类">' + __('批量隐藏') + '</button></li>',
                    '<li><button type="button" class="btn btn-success btn-batch-show" title="批量取消隐藏">' + __('批量显示') + '</button></li>'
                ]);
            }
                // 事件绑定
                $(".btn-group .dropdown-menu").off('click', '.btn-batch-hide').on('click', '.btn-batch-hide', function () {
                    console.log('Batch Hide Clicked'); // 调试信息
                    var selectedIds = Table.api.selectedids(table);
                  
                        console.log('Selected IDs:', selectedIds); // 调试信息
                        $.post("jeek/category/batch_hide", {ids: selectedIds}, function (res) {
                            if (res.code === 1) {
                                Layer.msg(res.msg, {icon: 1}); // 显示成功提示
                                table.bootstrapTable('refresh');
                            } else {
                                Layer.msg(res.msg, {icon: 2}); // 显示错误提示
                            }
                        });
                   
                });
                
                $(".btn-group .dropdown-menu").off('click', '.btn-batch-show').on('click', '.btn-batch-show', function () {
                    console.log('Batch Show  Clicked'); // 调试信息
                    var selectedIds = Table.api.selectedids(table);
                  
                    console.log('Selected IDs:', selectedIds); // 调试信息
                 
                        $.post("jeek/category/batch_show", {ids: selectedIds}, function (res) {
                            if (res.code === 1) {
                                Layer.msg(res.msg, {icon: 1}); // 显示成功提示
                                table.bootstrapTable('refresh');
                            } else {
                                Layer.msg(res.msg, {icon: 2}); // 显示错误提示
                            }
                        });
                });
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});